package com.top.servlet;

import com.top.bean.System_user;
import com.top.bean.User_detail_info;
import com.top.service.userService;
import com.top.service.userServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//注册
public class RegServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        userService service = new userServiceImpl();
        //获取页面传过来的所有参数
        String user_name = req.getParameter("user_name");
        String password = req.getParameter("password");
        String real_name = req.getParameter("real_name");
        int sex = Integer.parseInt(req.getParameter("sex"));
        String province = req.getParameter("province");
        String city = req.getParameter("city");
        int certify_type = Integer.parseInt(req.getParameter("certify_type"));
        String certify_num =  req.getParameter("certify_num");
        String birthday = req.getParameter("birthday");
        int type = Integer.parseInt(req.getParameter("type"));
        String remarks = req.getParameter("remarks");

        System_user user = new System_user(user_name,password);
        User_detail_info user_info = new User_detail_info(real_name,sex,province,city,certify_type,certify_num,birthday,type,remarks,0);
        boolean flag = service.reg(user, user_info);
        if (flag) {
            req.getSession().setAttribute("login", true);
            req.getRequestDispatcher("index.jsp").forward(req, resp);
        }else {
            resp.getWriter().print("注册失败");
        }
    }
}
